import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, NavigationExtras, Router } from '@angular/router';
import { Location } from '@angular/common';

import { AlertService } from '../../../common/alert';
// 服务
import { LoadingAnimateService } from '../../../common/loading';
import { EventListenService } from './eventlisten.service';

import { EventListen } from './eventlisten';

@Component({
    moduleId: module.id,
    templateUrl: 'eventlisten.edit.component.html',
    providers: [EventListenService]
})
export class EventlistenEditComponent implements OnInit {


    data: EventListen

    constructor(
        private router: Router,
        private route: ActivatedRoute,
        private loadingService: LoadingAnimateService,
        private eventListenService: EventListenService,
        private location: Location,
        private alert: AlertService
    ) { }

    ngOnInit(): void {
        this.data = new EventListen()
        this.data.from = "manager"
        this.route.queryParams.subscribe(params => {
            console.log(params);
            this.data.etype = params['etype'] ? decodeURI(params['etype']) : '';
            this.data.action = params['action'] ? decodeURI(params['action']) : '';
            this.data.from = params['from'] ? decodeURI(params['from']) : '';
            this.data.hookurl = params['hookurl'] ? params['hookurl'] : '';
        });
    }
    onSubmit(): void {
        this.edit();
    }
    edit(): void {
        this.loadingService.setValue(true);
        this.eventListenService.update(this.data).then(
            res => {
                this.loadingService.setValue(false);
                if (res.ok) {
                   
                    this.alert.showBlock('保存成功').subscribe(() => {
                        this.location.back();
                    });
                } else {
                     this.alert.show('修改失败'+ res.msg);
                }
            }
        ).catch(res => {
             this.alert.show('修改失败'+ res.msg);
        });
    }


}
